
<template>
  <Select :filterable="['AG_OUT','AG_IN','ZGX_SSDJ','JSGZ'].includes(code)" :multiple="isBatch" :placeholder="placeholder" v-model="key" @on-change="getVal" clearable style="width: 130px" :transfer=true>
    <Option v-for="(_item,index) in dictData" :key="index" :value="_item.code">{{_item.name}}</Option>
  </Select>
</template>

<script>
import { getDict } from "@/api/configure";
import _ from "lodash";
export default {
  name: "dict",
  props: ["code", "data", "multiple", "parentCode", "model", "value", "holder", "index", "kindCode"],
  data() {
    return {
      dictData: [],
      isBatch: this.multiple == null ? false : this.multiple,
      // isEdit: this.isEdit,
      source: this.data == null ? [] : this.data,
      key: this.value,
      placeholder: this.holder == null ? "请选择" : this.holder,
      levelCode: this.kindCode == null ? undefined : this.kindCode
    };
  },
  watch: {
    parentCode: function(newVal, oldVal) {
      let self = this;
      if (
        ["AG_IN", "ZGX_SSDJ", "ZGX_BSJB"].includes(self.code) &&
        newVal !== oldVal
      ) {
        self.getDataStart();
        self.key = null;
      }
    },
    value: function(newVal, oldVal) {
      this.key = newVal;
    },
  },
  methods: {
    getVal(val) {
      let self = this;
      if (val != null) {
        if (val instanceof Array) {
          let valStr = val.join("','");
          if (val.length > 0) {
            self.$emit("click", `self.${self.model}=['${valStr}']`);
          } else {
            self.$emit("click", `self.${self.model}=[]`);
          }
        } else if (self.code === 'ZGX_SSDJ' || self.code === 'ZGX_BSJB') {
          self.$emit("getCode", { code: self.code, val: val, index: self.index});
        } else {
          self.$emit("click", `self.${self.model}='${val}'`);
        }
      } else {
        self.$emit("click", `self.${self.model}=null`);
      }
      if (self.code === "AG_OUT") {
        self.$emit("getCode", { code: self.code, val: val });
      }
      if (self.code === "JSGZ") {
        let item = self.dictData.find(x => x.code === val);
        self.$emit("getCode", item.kindCode);
      }
    },
    getData() {
      let self = this;
      if (_.head(self.source)) {
        return Promise.resolve(self.source);
      }
      let dataJson = localStorage.getItem(self.$config.DICT_PREFIX + self.code);
      if (dataJson != null) {
        let data = JSON.parse(dataJson);
        return Promise.resolve(data);
      }

      return getDict({
        code: self.code,
        kindCode: self.parentCode,
        value: ''
      })
        .then(data => {
          return data;
        })
        .catch(error => {
          return Promise.reject(error);
        });
    },
    getDataStart() {
      let self = this;
      let { code } = self;
      self.getData().then(data => {
        if (code === "AG_OUT") {
          self.dictData = data.filter(x => x.hasChildren === "0");
        }
        // 计算规则
        else if ("JSGZ" === code) {
          self.dictData = data.map(x => ({
            name: x.ruleName,
            code: x.ruleId,
            kindCode: x.kindCode
          }));
          self.$emit("data", { code, data });
        }
        // 承办单位
        else if ("CBDW" === self.code) {
          self.dictData = data.map(x => ({
            name: x.teamName,
            code: x.teamId
          }));
        }
        // 比赛等级
        else if ("ZGX_BSJB" === self.code) {
          self.dictData = data.map(x => ({
            name: x.grandName,
            code: x.grandCode,
            kindCode: x.kindCode,
            ruleId: x.ruleId
          }));
        }
        // 比赛类型
        else if("ZGX_SSDJ" === self.code) {
          if(self.levelCode) {
            self.dictData = data.map(x => {
             if(self.levelCode == "-1" && (x.code.includes('3') || x.code.includes('4'))) {
              return {...x}
            } else if(self.levelCode == "0" && ((x.code.includes('5') || x.code.includes('6')) ||
            (x.code.includes('7') || x.code.includes('8'))) ){
              return { ...x }
            }
          });
           self.dictData = self.dictData && self.dictData.filter(x => !_.isEmpty(x));
          }
        }
        // 普通字典
        else {
          if (self.parentCode != null) {
            self.dictData = data.filter(x => x.parentCode === self.parentCode);
          } else {
            self.dictData = data.filter(x => _.isEmpty(x.parentCode));
          }
        }
      });
    }
  },
  mounted() {
    this.getDataStart();
  }
};
</script>

<style scoped>
</style>
